package yu.com.time

import yu.com.time.calclator.LocalTimeCalculator
import yu.com.time.unit.TimeUnit
import java.sql.Time
import java.time.LocalTime


/**
 * 对jdk8时间进行拓展
 * created zpf on 2018/10/23 0023
 */


/**
 * 转换到普通Time
 */
fun LocalTime.toTime(): Time = Time.valueOf(this)

/**
 * 设置时间部分
 * @param hour 设置小时部分,默认置为0，如果填写null则不改变
 * @param minute 设置分钟部分,默认置为0，如果填写null则不改变
 * @param second 设置秒部分,默认置为0，如果填写null则不改变
 * @param nano 设置纳秒部分,默认置为0，如果填写null则不改变
 */
fun LocalTime.withTime(hour: Int? = 0, minute: Int? = 0, second: Int? = 0, nano: Int? = 0): LocalTime {
    var time = this
    hour?.let { time = time.withHour(it) }
    minute?.let { time = time.withMinute(it) }
    second?.let { time = time.withSecond(it) }
    nano?.let { time = time.withNano(it) }
    return time
}


/**小时计算*/
val LocalTime.cHour get() = LocalTimeCalculator(this, TimeUnit.HOUR)

/**分钟计算*/
val LocalTime.cMinute get() = LocalTimeCalculator(this, TimeUnit.MINUTE)

/**秒钟计算*/
val LocalTime.cSecond get() = LocalTimeCalculator(this, TimeUnit.SECOND)

/**毫秒计算*/
val LocalTime.cMilli get() = LocalTimeCalculator(this, TimeUnit.MILLI)

/**纳秒计算*/
val LocalTime.cNano get() = LocalTimeCalculator(this, TimeUnit.NANO)